package com.jiinfo.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jiinfo.util.UUIDUtils;

public class Organ extends Model<Organ>{

	private static final long serialVersionUID = -2666494424119894962L;
	public static final Organ dao = new Organ();
	
	public Page<Record> pagequery(Integer pageNumber,Integer pageSize,Record record){
		StringBuffer sqlExceptSelect=new StringBuffer("from t_organ t where t.removed='0' ");
		List<Object> list=new ArrayList<Object>();
		
		
		sqlExceptSelect.append(" order by t.updateTime desc");
		
		if(pageNumber==null){
			pageNumber=1;
		}
		if(pageSize==null){
			pageSize=10;
		}
		return Db.paginate(pageNumber,pageSize,"select t.*",sqlExceptSelect.toString(),list.toArray());
	}
	
	public Organ add(Organ vo,Record user){
		Organ organ=new Organ();
		organ.set("id", UUIDUtils.getUUID());
		organ.set("createTime", new Date());
		organ.set("creator", user.getStr("id"));
		organ.set("updateTime", new Date());
		organ.set("updator", user.getStr("id"));
		organ.set("removed", "0");
		
		organ.set("name", vo.getStr("name"));
		organ.set("px",vo.getInt("px"));
		organ.set("parentId",vo.getStr("parentId"));
		
		organ.save();
		return organ;
	}
	
	public Organ edit(Organ vo,Record user){
		Organ organ=Organ.dao.findById(vo.getStr("id"));
		organ.set("updateTime", new Date());
		organ.set("updator", user.getStr("id"));
		
		organ.set("name", vo.getStr("name"));
		organ.set("px",vo.getInt("px"));
		organ.set("parentId",vo.getStr("parentId"));
		
		organ.update();
		return organ;
	}
	
	public Organ remove(String id){
		Organ organ=Organ.dao.findById(id);
		organ.set("removed", "1");
		organ.update();
		return organ;
	}
	
	public List<Record> getToporgans(){
		String sql="select r.* from t_organ r "
				+ " where r.removed='0' "
				+ " and (r.parentId is null or r.parentId='' )  "
				+ " order by r.px  ";
		return Db.find(sql);
	}
	
	public List<Record> getAllOrgansByParentId(String parentId){
		String sql="select r.* from t_organ r "
				+ " where r.removed='0' "
				+ " and r.parentId=? "
				+ " order by r.px  ";
		return Db.find(sql,parentId);
	}
	
	public Record getFullTreeOrgan(String childrenName,int parentLevel,Record record){
		List<Record> subList=getAllOrgansByParentId(record.getStr("id"));
		List<Record> list=new ArrayList<Record>();
		int subLevel=parentLevel+1;
		if(subList.size()>0){
			for (Record record2 : subList) {
				record.set("treeLevel", subLevel);
				list.add(getFullTreeOrgan(childrenName,subLevel,record2));
			}
		}
		record.set(childrenName, list);
		record.set("treeLevel", parentLevel);
		return record;
	}
	
	public List<Record> getTreeorgans(String childrenName){
		List<Record> list=getToporgans();
		int parentLevel=1;
		for (Record record : list) {
			getFullTreeOrgan(childrenName,parentLevel,record);
		}
		return list;
	}

}
